package copyengine.resource.resProxy.cacheData
{
    import copyengine.log.CELog;
    import copyengine.resource.resProxy.CEResFIleDesc;
    import copyengine.resource.resProxy.cacheData.create.ICEResCacheDataCreator;

    public class CEResCacheDataFactory
    {
        private static var mInstance:CEResCacheDataFactory;

        public static function get instance():CEResCacheDataFactory
        {
            if (mInstance == null) {
                mInstance = new CEResCacheDataFactory();
            }
            return mInstance;
        }

        private var mAllCacheDataCreatorVector:Vector.<ICEResCacheDataCreator>;

        public function CEResCacheDataFactory()
        {
            mAllCacheDataCreatorVector = new Vector.<ICEResCacheDataCreator>();
        }

        public function register(_creator:ICEResCacheDataCreator):void
        {
            mAllCacheDataCreatorVector.push(_creator);
        }

        /**
         * 根据资源文件类型创建对应的CEResCacheDataBasic
         * 注意资源文件的类型不一定为后缀名, 比如在Starling里面png结尾的文件有可能为纹理 也有可能仅仅是一张png图
         */
        public function createCEResCacheData(_resFileDesc:CEResFIleDesc):CEResCacheDataBasic
        {
            for each(var creator:ICEResCacheDataCreator in mAllCacheDataCreatorVector) {
                var cacheData:CEResCacheDataBasic = creator.tryToCreateCEResCacheData(_resFileDesc);
                if (cacheData != null) {
                    return cacheData;
                }
            }
            CELog.err("CEResCacheDataFactory -> Can't find any CEResCacheDataBasic fit for : " + _resFileDesc.toString());
            return null;
        }

    }

}
